﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SharePoint2007Migration.Costants;
using SharePoint2007Migration.Domain;
using System.Collections.Specialized;

namespace SharePoint2007Migration
{

    public class MissingWebPartIssue : BaseIssue
    {
        Guid _webPartId;

        public MissingWebPartIssue(IBaseIssue issue)
            : base(issue.Text)
        {
            // trick per concedere l' downcasting 
        }

        public Guid WebPartId 
        {
            get
            {
                if (_webPartId == Guid.Empty)
                    _webPartId = Get_webPartId();
                return _webPartId;
            }
        }

        private Guid Get_webPartId()
        {
            string webPartID = ExtractAndCleanupStringFromMessage(RegexCostants.REGEX_WEBPART_WEBPARTID, "WebPart class [", "]");
            return new Guid(webPartID);
        }

        public StringCollection GetPageUrlWhereWebPartIsInstalled(string ContentDbConnectionString)
        {
            StringCollection result = new StringCollection();
            List<Dictionary<string, object>> WebPartUrls = DataHelper.GetData(string.Format(ConfigHelper.QueryToGetMissingWpInfo,
                  this.WebPartId), ContentDbConnectionString);

            foreach (var item in WebPartUrls)
                result.Add((string)item["Url"]);
            return result;

        }

    }
}
